package com.dlwx.firefighting.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dlwx.firefighting.R;
import com.dlwx.firefighting.activity.MsgActivity;
import com.dlwx.firefighting.activity.SearchActivity;
import com.dlwx.firefighting.adapter.HomePageAdapter;
import com.dlwx.firefighting.adapter.MyPaperAdapter;
import com.dlwx.firefighting.bean.BaseBean;
import com.dlwx.firefighting.bean.LevelOneBean;
import com.dlwx.firefighting.config.Constant;
import com.dlwx.firefighting.fragment.home.AllClassifyFragment;
import com.dlwx.firefighting.fragment.home.HomeClassifyFragment;
import com.dlwx.firefighting.utils.DealResult;
import com.dlwx.firefighting.utils.ScreenUtils;
import com.dlwx.firefighting.utils.ToastUtils;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.google.gson.reflect.TypeToken;
import com.google.zxing.integration.android.IntentIntegrator;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.zhy.autolayout.AutoLinearLayout;
import com.zhy.autolayout.AutoRelativeLayout;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 首页fragment
 */
public class IndexFragment extends Fragment {

    @BindView(R.id.main_recyclerview)
    LRecyclerView mainRecyclerview;
    Unbinder unbinder;
    @BindView(R.id.rel_search)
    AutoLinearLayout relSearch;
    @BindView(R.id.img_scan)
    ImageView imgScan;
    @BindView(R.id.image_msg)
    ImageView imageMsg;
    @BindView(R.id.index_search_rl)
    AutoRelativeLayout indexSearchRl;
    @BindView(R.id.viewpaper)
    ViewPager viewpaper;
    @BindView(R.id.tab_layout)
    TabLayout tablayout;
    @BindView(R.id.image_more)
    ImageView imageMore;
    @BindView(R.id.chat)
    ImageView chat;
    @BindView(R.id.kefu)
    ImageView kefu;
    @BindView(R.id.phone)
    ImageView phone;
    private TabLayout tabLayout;
    private HomePageAdapter homePageAdapter;
    private boolean isRefresh = false;
    private MyPaperAdapter adapter;
    private AllClassifyFragment allClassifyFragment;
    private ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
    private PopupWindow popupWindow;
    private TagFlowLayout tagFlowLayout;

    public IndexFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_index, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();
        getDatas();
        return view;
    }


    private void getDatas() {
        OkGo.<String>post(Constant.parentlist)
                .tag(this)
//                .params("type", 1)
//                .params("num", 6)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        List<LevelOneBean> beans = DealResult.getInstance().dealDatas(getActivity(), response, new TypeToken<BaseBean<LevelOneBean,LevelOneBean>>() {
                        }.getType());
                        if(beans.isEmpty()){return;}
                        for (int i = 0; i < beans.size(); i++) {
                            int tid = beans.get(i).getTid();
                            Bundle bundle=new Bundle();
                            bundle.putInt("flag",tid);
                            if(i==0){
                                allClassifyFragment=new AllClassifyFragment();
                                allClassifyFragment.setArguments(bundle);
                                fragmentList.add(allClassifyFragment);
                            }else{
                                HomeClassifyFragment homeClassifyFragment=new HomeClassifyFragment();
                                homeClassifyFragment.setArguments(bundle);
                                fragmentList.add(homeClassifyFragment);
                            }
                        }
                        adapter = new MyPaperAdapter(getActivity().getSupportFragmentManager(), beans, fragmentList);
                        viewpaper.setAdapter(adapter);
                        tablayout.setupWithViewPager(viewpaper, true);

//
                        tagFlowLayout.setAdapter(new TagAdapter(beans) {
                            @Override
                            public View getView(FlowLayout parent, int position, Object o) {
                                TextView textView = (TextView) LayoutInflater.from(getActivity())
                                        .inflate(R.layout.tv, tagFlowLayout, false);
                                textView.setText(((LevelOneBean)o).getName());
                                return textView;
                            }
                        });
                        tagFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
                            @Override
                            public boolean onTagClick(View view, int position, FlowLayout parent) {
                                tablayout.setScrollPosition(position,0,true);
                                viewpaper.setCurrentItem(position);
                                popupWindow.dismiss();
                                return false;
                            }
                        });
                    }

                    @Override
                    public void onError(Response<String> response) {
                        ToastUtils.toastForShort(getActivity(), getString(R.string.bad_net));
                        super.onError(response);
                    }
                });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    public void initView() {
        popupWindow = new PopupWindow(getActivity());
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.popup_more, null);
        int width = ScreenUtils.getScreenWidth(getActivity());
        popupWindow.setWidth(width);
        popupWindow.setContentView(view);
        popupWindow.setOutsideTouchable(true);
        tagFlowLayout = (TagFlowLayout) view.findViewById(R.id.tag_layout);
    }


    @OnClick({R.id.img_scan, R.id.rel_search, R.id.image_more, R.id.kefu,R.id.image_msg})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_scan:
                IntentIntegrator integrator = new IntentIntegrator(getActivity());
                integrator.setOrientationLocked(false);
                integrator.initiateScan();
                break;
            case R.id.rel_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.image_more:
                popupWindow.showAsDropDown(imageMore);
                break;
            case R.id.image_msg:
                startActivity(new Intent(getActivity(),MsgActivity.class));
                break;
            case R.id.kefu:
                if(phone.isShown()){
                    phone.setVisibility(View.GONE);
                    chat.setVisibility(View.GONE);
                }else{
                    phone.setVisibility(View.VISIBLE);
                    chat.setVisibility(View.VISIBLE);
                }
                break;
        }
    }

}
